package com.hengpeng.msls.service.activity.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.itfinbase.en.RewardRedPacketStatus;
import com.hengpeng.itfinbase.en.RewardType;
import com.hengpeng.itfinbase.en.RewardUseType;
import com.hengpeng.itfinbase.persist.Reward;
import com.hengpeng.itfinbase.persist.RewardRedpacket;
import com.hengpeng.msls.service.BaseService;
import com.hengpeng.msls.service.activity.RedPacketService;
import com.hengpeng.msls.vo.activity.RewardRedpacketVo;

@Lazy
@Service
public class RedPacketServiceImpl extends BaseService implements RedPacketService {

    @Override
    public ServiceResult<List<RewardRedpacket>> queryMyList(Long userId,RewardRedPacketStatus[] status, Integer curPage, Integer pageSize) {
        DynamicQuery query = new DynamicQuery(RewardRedpacket.class);
        if (userId != null) {
            query.eq("userId", userId);
        }
        if (status != null){
            query.inArray("status", status);
        }
        query.desc("createTime");
        query.setPageIndex(curPage);
        query.setPageSize(pageSize);
        query.setIsSearchTotalCount(true);
        query.setQueryType(QueryType.JDBC);
        QueryResult<RewardRedpacket> result = serviceFactory.getProtFactory().remoteQuery(query, RewardRedpacket.class);

        Long count = result.getTotalCount();
        ServiceResult<List<RewardRedpacket>> serviceResult = new ServiceResult<List<RewardRedpacket>>();// 设置返回值
        serviceResult.setReturnValue(result.getResults());
        serviceResult.setReturnListSize(count.intValue());
        return serviceResult;
    }
    
    @Override
    public ServiceResult<List<RewardRedpacketVo>> queryMyVoList(Long userId,RewardRedPacketStatus[] status, Integer curPage, Integer pageSize) {
        
        List<RewardRedpacketVo> dataList = new ArrayList<RewardRedpacketVo>();
        ServiceResult<List<RewardRedpacketVo>> serviceResult = new ServiceResult<List<RewardRedpacketVo>>();// 设置返回值
        DynamicQuery query = new DynamicQuery(RewardRedpacket.class,"red");
        query.createAlias(Reward.class, "rew");
        query.eqProperty("red.rewardId", "rew.id");
        query.addProjection(Projections.property("red.id"));//0
        query.addProjection(Projections.property("red.money"));//1
        query.addProjection(Projections.property("red.status"));//2
        query.addProjection(Projections.property("red.endtime"));//3
        query.addProjection(Projections.property("red.updateTime"));//4
        query.addProjection(Projections.property("red.rewardId"));//5
        query.addProjection(Projections.property("rew.rewardType"));//6
        query.addProjection(Projections.property("rew.rewardUseType"));//7
        query.addProjection(Projections.property("red.activeid"));//8
        query.addProjection(Projections.property("rew.rewardName"));//9
        if (userId != null) {
            query.eq("red.userId", userId);
        }
        if (status != null){
            query.inArray("red.status", status);
        }
        query.desc("red.createTime");
        query.setPageIndex(curPage);
        query.setPageSize(pageSize);
        query.setIsSearchTotalCount(true);
        query.setQueryType(QueryType.JDBC);
        
        QueryResult result = serviceFactory.getProtFactory().remoteQuery(query, null);
        Long count = result.getTotalCount();
        if (result.getResults() != null && result.getResults().size() > 0) {
            for (int i = 0; i < result.getResults().size(); i++) {
                Object[] obj = (Object[]) result.getResults().get(i);
                RewardRedpacketVo vo = new RewardRedpacketVo();
                vo.setId(Long.parseLong(String.valueOf(obj[0])));
                vo.setMoney(Long.parseLong(String.valueOf(obj[1])));
                vo.setStatus(RewardRedPacketStatus.valueOf(String.valueOf(obj[2])));
                vo.setEndtime(DateTimeUtil.formatToDate(String.valueOf(obj[3]), DateTimeUtil.DATE_FORMAT_YMDHMS));
                vo.setUpdateTime(DateTimeUtil.formatToDate(String.valueOf(obj[4]), DateTimeUtil.DATE_FORMAT_YMDHMS));
                vo.setRewardId(Long.parseLong(String.valueOf(obj[5])));
                vo.setRewardType(RewardType.valueOf(String.valueOf(obj[6])));
                vo.setRewardUseType(RewardUseType.valueOf(String.valueOf(obj[7])));
                vo.setActiveid(Long.parseLong(String.valueOf(obj[8])));
                vo.setRewardName(String.valueOf(obj[9]));
                dataList.add(vo);
            }
        }
        
        serviceResult.setReturnValue(dataList);
        serviceResult.setReturnListSize(count.intValue());
        return serviceResult;
    }

    @Override
    public Long countRedPacket(Long userId, RewardRedPacketStatus[] status) {
        Long count = 0L;
        DynamicQuery query = new DynamicQuery(RewardRedpacket.class);
        query.addProjection(Projections.sum("money"));
        if (userId != null) {
            query.eq("userId", userId);
        }
        if (status != null){
            query.inArray("status", status);
        }
        query.setQueryType(QueryType.JDBC);
        System.out.println(query.getSqlResult().getSql());
        List<Object> list = serviceFactory.getProtFactory().findObject(null, query);
        BigDecimal a = (BigDecimal) list.get(0);
        if(a != null){
            count = a.longValue();
        }
        return count;
    }
}